package com.century.sjt.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description:银行卡详情
 * @date : 2016/8/21 20:02
 */
public class BankDataAcitvity extends BaseActivity{
    private LinearLayout layoutBank,layoutStatus;
    private Button btn_bank_delete;
    private String phone,bankCardNo,bankCode,bankName,bankType,status;
    private TextView tvBankName,tvCardType,tvBankCardNo,tvPhome;
    private ImageView tvBankImage;
    private SjtTitleBar bankDataTitle;
    private RequestQueue mQueue;
    private TextView QuickPay;
    private LinearLayout layout_BankCard_bg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_bank_card_data);

        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            phone = bundle.getString("phone");
            bankCardNo = bundle.getString("bankCardNo");
            bankCode = bundle.getString("bankCode");
            bankName = bundle.getString("bankName");
            bankType = bundle.getString("bankType");
            status = bundle.getString("status");
        }
        mQueue = Volley.newRequestQueue(this);
        initView();
        initEvent();


    }

    @Override
    protected void initView() {
        bankDataTitle= (SjtTitleBar) findViewById(R.id.bank_data_title);
        bankDataTitle.setTitle(getResources().getString(R.string.tab_bankcard));
        bankDataTitle.setLeftIconResource(getResources().getString(R.string.icon_return));
        bankDataTitle.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_bank_delete= (Button) findViewById(R.id.btn_bank_delete);
        tvBankName = (TextView) findViewById(R.id.tv_bankname);
        tvCardType = (TextView) findViewById(R.id.tv_cardtype);
        tvBankCardNo = (TextView) findViewById(R.id.tv_bankcardno);
        tvBankImage = (ImageView) findViewById(R.id.a_yh_image);
        layoutStatus = (LinearLayout) findViewById(R.id.btn_bank_status);
        QuickPay = (TextView) findViewById(R.id.bankcard_quick);
        tvPhome = (TextView) findViewById(R.id.tv_phome);
        layout_BankCard_bg = (LinearLayout) findViewById(R.id.layout_bankcard_bg);


    }

    @Override
    protected void initEvent() {
            //       标题栏

        tvBankName.setText(bankName);
        switch (status){
            case "0":
                String Type1 = " ("+getResources().getString(R.string.text_general_pay)+")";
                tvCardType.setText("("+bankType+")");
                QuickPay.setText(getResources().getString(R.string.text_no_open));
                layoutStatus.setEnabled(true);
                //layoutStatus.setVisibility(View.VISIBLE);
                break;
            case "1":
                String Type2 = " ("+getResources().getString(R.string.text_quicker_pay)+")";
                QuickPay.setText(getResources().getString(R.string.text_open));
                tvCardType.setText("("+bankType+")");
                layoutStatus.setEnabled(false);
                break;
            default:
                tvCardType.setText(bankType);
                break;
        }
        String cardNo=bankCardNo;
        cardNo=cardNo.substring(cardNo.length()-4,cardNo.length());
        tvBankCardNo.setText(cardNo);
        tvPhome.setText(phone.substring(phone.length() - 4, phone.length()));

//        默认图标
        tvBankImage.setImageResource(R.mipmap.yl);

        String[] bankCodes = getResources().getStringArray(R.array.bankcode);
        TypedArray ar = getResources().obtainTypedArray(R.array.bankicon);
        TypedArray bg = getResources().obtainTypedArray(R.array.bankcard_bg);
        for(int i = 0;i<bankCodes.length;i++){
//           Log.e("bankCodes",""+(bankCodes[i].toString()==mDataList.get(position).getBankCode().toString()));
            if(bankCodes[i].toString().equals(bankCode)){
                tvBankImage.setImageResource(ar.getResourceId(i, 0));
                layout_BankCard_bg.setBackgroundResource(bg.getResourceId(i,0));
//               Log.e("ar",""+ar.getResourceId(i, 0));
                break;
            }else {
                tvBankImage.setImageDrawable(getResources().getDrawable(R.drawable.bankicon_yl));
                layout_BankCard_bg.setBackgroundDrawable(getResources().getDrawable(R.drawable.bankbg_other));
            }
           }
        btn_bank_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//询问是否取消
                     AlertDialog.Builder builder = new AlertDialog.Builder(BankDataAcitvity.this);
                    builder.setTitle(getResources().getString(R.string.text_prompt));
                    builder.setMessage(getResources().getString(R.string.error_is_delete_bankcard));
                    builder.setPositiveButton(getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            unbindBankCard();
                        }
                    });
                    builder.setNegativeButton(getResources().getString(R.string.btn_no), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setCancelable(false);
                    builder.create().show();

                }
            });
        layoutStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(BankDataAcitvity.this, AddBankActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("phone",phone);
                    bundle.putString("bankCardNo",bankCardNo);
                    bundle.putString("bankCode", bankCode);
                    bundle.putString("bankName", bankName);
                    bundle.putString("bankType",bankType);
                    bundle.putString("status",status);
                    intent.putExtras(bundle);
                    intent.putExtra("type","5");
                    startActivity(intent);
                }
            });

    }
    /**
     * 解除绑定银行卡
     */
    private void unbindBankCard() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), BankDataAcitvity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.GetUnbindBankCard, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("解除绑定银行卡接口成功返回", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
//                        TipUtil.showToast(getResources().getString(R.string.text_delte_success), BankDataAcitvity.this, 1);
                        finish();

                    } else {//失败
                        TipUtil.showToast(message, BankDataAcitvity.this, 1);
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), BankDataAcitvity.this, 1);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), BankDataAcitvity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("bankCardNo", bankCardNo);
                return map;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
//                Log.e("cookie", cookieValue);
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);

    }

    @Override
    protected void initData() {

    }
}
